package downsampler;
import java.io.BufferedWriter;
import java.io.FileWriter;

import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

/**
 * 
 */

/**
 * @author Dan
 *
 */
public class Driver {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		System.out.println("Reading in data...");
		long start = System.currentTimeMillis();
        DataSource source = new DataSource("C:/Users/Dan/Documents/My Dropbox/2011 Fall Class (Emotional Speech)/ALC/IS11.TRAIN_f.arff");
        Instances data = source.getDataSet();
        DataSource ids = new DataSource("C:/Users/Dan/Documents/My Dropbox/2011 Fall Class (Emotional Speech)/ALC/BAC.TRAIN.arff");
        Instances idData = ids.getDataSet();
        System.out.println("data read in " + (System.currentTimeMillis()-start)/1000 + " seconds");
        
        int numSober = 0;
        int numIntox = 0;
        int classIndex = data.numAttributes()-1;
        for(int i=0; i<data.numInstances(); i++) {
        	if(data.instance(i).stringValue(classIndex).equals("NAL"))
        		numSober++;
        	else
        		numIntox++;
        }
        
        System.out.println("Initial - Sober: " + numSober + ", Intox: " + numIntox);
        
        int numExtraSober = numSober - numIntox;
        for(int i=0; i<numExtraSober; i++)
        {
        	int index = (int)(Math.random() * data.numInstances());
        	if(data.instance(index).stringValue(classIndex).equals("NAL")) {
        		data.delete(index);
        		idData.delete(index);
        	}
        	else {
        		i--;
        	}
        }
        
        numSober = 0;
        numIntox = 0;
        for(int i=0; i<data.numInstances(); i++) {
        	if(data.instance(i).stringValue(classIndex).equals("NAL"))
        		numSober++;
        	else
        		numIntox++;
        }
        
        System.out.println("Final - Sober: " + numSober + ", Intox: " + numIntox);
        
        System.out.println("Writing balanced data set");
        BufferedWriter writer = new BufferedWriter( new FileWriter("C:/Users/Dan/Documents/My Dropbox/2011 Fall Class (Emotional Speech)/ALC/IS11.TRAIN_fb.arff"));
        writer.write(data.toString());
        writer.newLine();
        writer.flush();
        writer.close();
        
        writer = new BufferedWriter( new FileWriter("C:/Users/Dan/Documents/My Dropbox/2011 Fall Class (Emotional Speech)/ALC/BAC.TRAIN_b.arff"));
        writer.write(idData.toString());
        writer.newLine();
        writer.flush();
        writer.close();
        System.out.println("Done writing");
	}
}
